#include <iostream>
#include <string>
#include "vpm_algorithm.h"
#include "vpm_file.h"
#include "vpm_utils.h"

#define HELPMSG "Usage : vpm [OPTIONS]...\nPrint a message randomly through a database\n\nCall program without any options will simply print a message randomly with the config enabled\n  -c, --create <title> <description>   make a new message\n  -r, --remove <title>    remove a message\n  -t, --target <title>    show a targeted message\n  -m, --edit <title>    edit a message\n  -l, --list <max-items>  list all the messages,\n                             the default limit of items to show is 50\n  -i, --import       import database from a file\n  -e, --export       simply copy the actual database to a directory\n\n  -h, --help       print help informations and leave\n  -v, --version    print version and leave\n\n  -d, --debug      print debug informations while running\n  -y, --auto-confirmation       automatically confirm\n                                   when prompt to yes or no"
#define VERSIONMSG "VotrePhraseMonsieur 0.0.1"


int main(int argc,char** argv){

  std::string arg;
  bool debug=false;
  int items;
  const int realargc=argc-1;
  
  // Nestable commands are priotirized

  for(int i=1;i<argc;i++){
  
    arg=argv[i];
     
    if(arg=="-d"||arg=="--debug"){
    
      debug=true;
    
    }
    
  }
  
  for(int i=1;i<argc;i++){
  
    arg=argv[i];
    
    if(arg=="-h"||arg=="--help"){
    
      std::cout<<HELPMSG<<std::endl;
      return 0;
    
    }
    
    else if(arg=="-v"||arg=="--version"){
    
      std::cout<<VERSIONMSG<<std::endl;
      return 0;
    
    }
    
    else if(arg=="-t"||arg=="--target"){
    
      if(ArgHandler(realargc,i,1)){
      
        std::cout<<"vpm : missing argument\nWrite « vpm --help » for more informations"<<std::endl;
        return -1;
      
      }
    
      return 0;
    
    }
    
    else if(arg=="-l"||arg=="--list"){
      
      if(ArgHandler(realargc,i,1)){
      
        std::cout<<"vpm : missing argument\nWrite « vpm --help » for more informations"<<std::endl;
        return -1;
      
      }
      
      return 0;
    
    }
    
    else if(arg=="-c"||arg=="--create"){
    
      if(ArgHandler(realargc,i,2)){
      
        std::cout<<"vpm : missing argument\nWrite « vpm --help » for more informations"<<std::endl;
        return -1;
      
      }
    
      if(Search(argv[i+1])==0){
      
        std::cout<<"vpm : there's already a title named « "<<argv[i+1]<<" »"<<std::endl;
        return -1;
      
      }
      
      Create(argv[i+1],argv[i+2]);
      return 0;
    
    }
    
    else if(arg=="-r"||arg=="--remove"){
    
      if(ArgHandler(realargc,i,1)){
      
        std::cout<<"vpm : missing argument\nWrite « vpm --help » for more informations"<<std::endl;
        return -1;
      
      }
    
      return 0;
    
    }
    
     else if(arg=="-m"||arg=="--edit"){
    
      if(ArgHandler(realargc,i,1)){
      
        std::cout<<"vpm : missing argument\nWrite « vpm --help » for more informations"<<std::endl;
        return -1;
      
      }
      
      return 0;
    
     }
    
    else if(arg!=""){
    
      std::cout<<"vpm : invalid argument -- '"<<arg<<"'\nWrite « vpm --help » for more informations"<<std::endl;
      return -1;
    
    }
  
  }
  
  items=MaxItems();
  
  if(items==0){
  
    std::cout<<"vpm : no messages have been found, try creating one\nWrite « vpm --help » for more informations"<<std::endl;
    return 0;
  
  }
  
  std::cout<<Show::Number(RandomNumber(items))<<std::endl;
  return 0;

}
